package com.qiuzhiyi.activity;

import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.li.widget.TopNavigationBar;
import com.qiuzhiyi.R;
import com.qiuzhiyi.adapter.MsgListAdapter;
import com.qiuzhiyi.base.QZYBaseActivity;
import com.qiuzhiyi.item.BaseItem;
import com.qiuzhiyi.item.MsgListItem;
import com.qiuzhiyi.net.ObservableHelper;
import com.qiuzhiyi.net.RetrofitHelper;
import com.qiuzhiyi.net.SelectorObserver;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.disposables.Disposable;

public class MsgListActivity extends QZYBaseActivity {


    @BindView(R.id.top_navigation)
    TopNavigationBar mTopNavigation;
    @BindView(R.id.recycler_msg)
    RecyclerView mRecyclerMsg;


    private List<MsgListItem> mMsgListItems;
    private MsgListAdapter mMsgListAdapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_msg_list;
    }

    @Override
    protected void initSth() {
        super.initSth();
        mTopNavigation.setNavigationBarListener(this);
        String id = getIntent().getStringExtra("id");
        mTopNavigation.setTitle(getIntent().getStringExtra("title"));
        mMsgListAdapter = new MsgListAdapter(R.layout.item_msg_list);
        mRecyclerMsg.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerMsg.addItemDecoration(new DividerItemDecoration(mContext, DividerItemDecoration.VERTICAL));
        mMsgListAdapter.bindToRecyclerView(mRecyclerMsg);
        mMsgListAdapter.setEmptyView(R.layout.layout_msg_list_empty);
        showMsgList(id);
        mMsgListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Bundle bundle=new Bundle();
                bundle.putString("newsId", String.valueOf(mMsgListItems.get(position).getId()));
                startWithArg(MsgDetailActivity.class,bundle);
            }
        });
    }

    private void showMsgList(final String id) {
        Map<String, String> map = new HashMap<>();
        map.put("newsClass", id);
        ObservableHelper.createObservable(RetrofitHelper.createService().getMsgList(map))
                .subscribe(new SelectorObserver<BaseItem<List<MsgListItem>>>() {
                    @Override
                    protected void onErrorCodeInfo(String code, String info) {
                        showToast(info);
                    }

                    @Override
                    protected void onSuccess(BaseItem<List<MsgListItem>> item) {
                        mMsgListItems = item.getObjValue();
                        mMsgListAdapter.setNewData(mMsgListItems);
                    }

                    @Override
                    protected void onFinish() {
                        dismissLoadingDialog();
                    }

                    @Override
                    public void onSubscribe(Disposable d) {
                        addDisposable(d);
                        showLoadingDialog();
                    }
                });
    }

}
